package cn.tedu.charging.user.repository;

import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingUserVehicleBindPO;
import cn.tedu.charging.common.pojo.po.ChargingVehiclePO;
import cn.tedu.charging.common.pojo.vo.BalanceVO;
import cn.tedu.charging.common.pojo.vo.VehicleVO;
import cn.tedu.charging.user.mapper.ChargingUserVehicleBindMapper;
import cn.tedu.charging.user.mapper.UserMapper;
import cn.tedu.charging.user.mapper.VehicleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepository {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ChargingUserVehicleBindMapper vehicleBindMapper;
@Autowired
private VehicleMapper vehicleMapper;
    public ChargingUserInfoPO getUserByOpenId(String openId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("wx_open_id",openId);
        ChargingUserInfoPO po = userMapper.selectOne(wrapper);
        return po;
    }

    public void save(ChargingUserInfoPO po) {
        userMapper.insert(po);
    }


    public ChargingUserInfoPO getBalance(Integer userId) {
        return userMapper.selectById(userId);
    }


    public void updateByUserId(ChargeParam chargeParam) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",chargeParam.getUserId());
        updateWrapper.setSql("balance=balance+" + chargeParam.getAmount());
        userMapper.update(null,updateWrapper);
    }


    public List<ChargingUserVehicleBindPO> getBindedVehicle(Integer userId, Integer state) {
        QueryWrapper<ChargingUserVehicleBindPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("state",state);
        List<ChargingUserVehicleBindPO>  chargingUserVehicleBindPO =vehicleBindMapper.selectList(wrapper);
        return chargingUserVehicleBindPO;
    }

    public ChargingVehiclePO getVehicleById(Integer vehicleId) {
        ChargingVehiclePO vehiclePO = vehicleMapper.selectById(vehicleId);
        return vehiclePO;
    }

    public ChargingVehiclePO getVehicleByLicense(String license) {
        QueryWrapper<ChargingVehiclePO> wrapper = new QueryWrapper<>();
        wrapper.eq("license",license);
        ChargingVehiclePO chargingVehiclePO = vehicleMapper.selectOne(wrapper);
        return chargingVehiclePO;
    }

    public void insertVehicle(ChargingVehiclePO vehiclePO) {
        vehicleMapper.insert(vehiclePO);
    }

    public void deleteBindedVehicle(Integer userId, Integer vehicleId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("vehicle_id",vehicleId);
        vehicleBindMapper.delete(wrapper);
    }

    public void bindVehicle(ChargingUserVehicleBindPO chargingUserVehicleBindPO) {
        vehicleBindMapper.insert(chargingUserVehicleBindPO);
    }
}
